package com.hzdl.apple.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSourceFactory;

/**
 * JDBC的简单封装
 */
public class DruidUtils {

	// 数据库的连接
	public static Connection conn;
	public static DataSource dataSource;

	static {
		try {
			Properties properties = new Properties();
			properties.load(DruidUtils.class.getResourceAsStream("/dbdruid.properties"));
			dataSource = DruidDataSourceFactory.createDataSource(properties);
			conn = dataSource.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取一个数据库连接
	 * 
	 * @return
	 */
	public static Connection getConnect() {
		return conn;
	}

	/**
	 * 关闭数据库连接
	 */
	public static void close(Object... objs) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		try {
			for (Object object : objs) {
				if (object instanceof Statement) {
					if (object != null) {
						((Statement) object).close();
					}
				}

				if (object instanceof ResultSet) {
					if (object != null) {
						((ResultSet) object).close();
					}
				}
			}
		} catch (Exception e) {

		}

	}

}
